<template>
  <!-- 管理员信息 -->
  <div>
    <!-- 标题组件 -->
    <panel title="管理员信息">
      <div class="content">

        <template>
          <div v-if="userInfo">
            <span>管理员ID： {{ userInfo.id }} </span>
            <el-divider></el-divider>
            <span>账号： {{ userInfo.account  }} </span>
            <el-divider></el-divider>
            <span>用户组： {{ userInfo.userGroup }} </span>
            <el-divider></el-divider>
            <span>创建时间： {{ userInfo.ctime | filterTime }} </span>
            <el-divider></el-divider>

            <div class="imgBox">
            <span>管理员头像： </span>
            <el-upload
              class="avatar-uploader" 
              :action="baseUrl+'/users/avatar_upload?id='+userInfo.id"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload> 
            </div>
            
          </div>
        </template>
      </div>
    </panel>

  </div>
</template>


<script>
// 导入公共标题组件
import Panel from "../../components/Panel.vue";
import {accountInfo} from '@/api/user.js'

export default {
  data() {
    return {
      imageUrl: "",
      userInfo:'',
      baseUrl:"http://localhost:5000",  //定义地址
    };
  },
  // 注册组件
  components: {
    Panel,
  },

  methods:{
    // 成功之后的回调函数
      handleAvatarSuccess(res,file,fileList){
        console.log(res);
        this.imageUrl=this.baseUrl+res.imgUrl;
        // 成功之后 中央事件总线 跨组件更新头像
        this.$bus.$emit('changImg');
      },
        // 上传之前的回调函数
        beforeAvatarUpload(){
            this.getUserInfo();
        },

   async getUserInfo(){
        // 获取本地用户ID
        let {id} =JSON.parse(localStorage.getItem('user'));
        let res =await accountInfo({id});
        this.userInfo=res.data.accountInfo;
        this.imageUrl=res.data.accountInfo.imgUrl;
    },
  },

// 进入页面请求列表
  created(){
    this.getUserInfo();
  },

    filters: {
    // 时间函数
    filterTime(timeStr) {
      let date = new Date(timeStr);
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      let d = date.getDate();
      let h = date.getHours();
      let mini = date.getMinutes();
      let s = date.getSeconds();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      mini = mini < 10 ? "0" + mini : mini;
      s = s < 10 ? "0" + s : s;
      return `${y}-${m}-${d} ${h}:${mini}:${s}`;
    },
  },

};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .imgBox{
      display: flex;
  }
  .avatar-uploader{
      border: 1px dashed rgb(187, 187, 187);
  }
</style>